package com.ardf.service.impl;

import com.ardf.entity.Competition;
import com.ardf.entity.Details;
import com.ardf.entity.Grade;
import com.ardf.entity.Print;
import com.ardf.entity.dto.AthleteTeamDto;
import com.ardf.service.*;
import com.ardf.util.PrintGrade;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.BiPredicate;

@Service
public class PrintServiceImpl implements PrintService {

    @Resource
    private ICompetitionService competitionService;

    @Resource
    private ICardService cardService;

    @Resource
    private IGradeService gradeService;

    @Resource
    private IDetailsService detailsService;

    @Resource
    private IAthleteTeamService teamService;

    public Boolean printPerson(Long CompetitionId, String project, Long cardId, Long projectId) {
        Print printGrade = new Print();
        Boolean result = false;
        //路线
        List<String> stations = new ArrayList<>();
        //时间
        List<String> times = new ArrayList<>();
        //次序
        List<String> sequences = new ArrayList<>();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");

        Competition competition = competitionService.selectById(CompetitionId);
        printGrade.setCompetitionName(competition.getName());
        printGrade.setCompetitionOrganization(competition.getOrganization());
        printGrade.setProject(project);
        printGrade.setDate(LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy年MM月dd日")));

        Print athPrint = this.cardService.selectPrintById(cardId);
        printGrade.setGroup(athPrint.getGroup());
        printGrade.setCard(athPrint.getCard());
        printGrade.setAthleteName(athPrint.getAthleteName());
        printGrade.setNumber(athPrint.getNumber());

        Grade grade = this.gradeService.selectByProIdAndAthId(projectId, athPrint.getAthleteId());
        if (grade.getRoute()!=null || grade.getTime()!=null ) {

            printGrade.setTime(grade.getTime().format(formatter));

            //根据成绩id查询打卡路线详情
            List<Details> details = this.detailsService.selectByGId(grade.getId());
            printGrade.setCount(details.size());

            for (Details detail : details) {
                if(detail.getStationNumber()==0){
                    stations.add("起点");
                }else if(detail.getStationNumber()>=1 && detail.getStationNumber()<=10){
                    stations.add(String.valueOf(detail.getStationNumber()));
                }
                else if(detail.getStationNumber()>=11 && detail.getStationNumber()<=15){
                    stations.add("F" + detail.getStationNumber()%10);
                }
                else if(detail.getStationNumber()==19){
                    stations.add("S");
                }
                else if(detail.getStationNumber()==20){
                    stations.add("MO");
                }
                else if(detail.getStationNumber()==99){
                    stations.add("终点");
                }
                sequences.add(String.valueOf(detail.getOrder()));
                times.add(detail.getClockTime().format(formatter));
            }
            printGrade.setRoutes(stations);    //保存打卡路线
            printGrade.setTimeList(times);
            printGrade.setSequences(sequences);
        }
        PrintGrade gp = new PrintGrade(printGrade,false);
        result = gp.printGrade();
        return result;
    }

    @Override
    public Boolean printTeam( Long CompetitionId, String project, Long teamId, Long projectId) {
        Print printGrade = new Print();
        Boolean result = false;
        //路线
        List<String> stations = new ArrayList<>();
        //时间
        List<String> times = new ArrayList<>();
        //次序
        List<String> sequences = new ArrayList<>();
        //接力选手集合
        List<String> names=new ArrayList<>();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");

        Competition competition = competitionService.selectById(CompetitionId);
        printGrade.setCompetitionName(competition.getName());
        printGrade.setCompetitionOrganization(competition.getOrganization());
        printGrade.setProject(project);
        printGrade.setDate(LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy年MM月dd日")));

        List<AthleteTeamDto> athleteTeamDtos = teamService.selectPrintAthleteByTeamId(teamId,projectId);
        for (AthleteTeamDto dto:athleteTeamDtos){
            names.add(dto.getAthleteName());
        }
        printGrade.setAthleteNames(names); //接力赛的选手信息
        printGrade.setTeamName(athleteTeamDtos.get(0).getTeamName());
        printGrade.setGroup(athleteTeamDtos.get(0).getGroupName());

        Grade grade = this.gradeService.selectByProIdAndTId(projectId, teamId);

        if (grade.getRoute()!=null || grade.getTime()!=null ) {

            printGrade.setTime(grade.getTime().format(formatter));

            //根据成绩id查询打卡路线详情
            List<Details> details = this.detailsService.selectByGId(grade.getId());
            printGrade.setCount(details.size());

            for (Details detail : details) {
                if(detail.getStationNumber()==0){
                    stations.add("起点");
                }else if(detail.getStationNumber()>=1 && detail.getStationNumber()<=10){
                    stations.add(String.valueOf(detail.getStationNumber()));
                }
                else if(detail.getStationNumber()>=11 && detail.getStationNumber()<=15){
                    stations.add("F" + detail.getStationNumber()%10);
                }
                else if(detail.getStationNumber()==19){
                    stations.add("S");
                }
                else if(detail.getStationNumber()==20){
                    stations.add("MO");
                }
                else if(detail.getStationNumber()==99){
                    stations.add("终点");
                }
                sequences.add(String.valueOf(detail.getOrder()));
                times.add(detail.getClockTime().format(formatter));
            }
            printGrade.setRoutes(stations);    //保存打卡路线
            printGrade.setTimeList(times);
            printGrade.setSequences(sequences);
        }
        PrintGrade gp = new PrintGrade(printGrade,true);
        result = gp.printGrade();

        return result;
    }
}
